package org.gwu.seuss.admin.commandline;

import org.gwu.seuss.ui.commandline.BaseCommandLineArgs;
import org.gwu.seuss.ui.validator.RequestValidator;

public class AdminValidator extends RequestValidator {

	@Override
	public <T extends BaseCommandLineArgs> boolean isValid(T cli) {
		AdministratorCommandLineArgs cla = (AdministratorCommandLineArgs) cli;
		if (tooManyArguments(cla)) {
			errorMessage = "too many arguments";
			return false;
		}
		if (createNewUser(cla)) {
			errorMessage = "new user requires username and password";
			return false;
		}
		return true;

	}

	private boolean createNewUser(AdministratorCommandLineArgs cla) {
		return (cla.getNewPassword() != null && cla.getNewUsername() == null)
				|| (cla.getNewPassword() == null && cla.getNewUsername() != null);
	}

	private boolean tooManyArguments(AdministratorCommandLineArgs cla) {

		if (cla.getDisableUsername() != null
				&& (cla.getRemoveUsername() != null
						|| cla.getEnableUsername() != null
						|| cla.getNewPassword() != null || cla.getNewUsername() != null)) {
			return true;
		} else if (cla.getEnableUsername() != null
				&& (cla.getRemoveUsername() != null
						|| cla.getDisableUsername() != null
						|| cla.getNewPassword() != null || cla.getNewUsername() != null)) {
			return true;
		} else if (cla.getNewPassword() != null
				&& (cla.getRemoveUsername() != null
						|| cla.getDisableUsername() != null || cla
						.getEnableUsername() != null)) {
			return true;
		} else if (cla.getNewUsername() != null
				&& (cla.getRemoveUsername() != null
						|| cla.getDisableUsername() != null || cla
						.getEnableUsername() != null)) {
			return true;
		} else if (cla.getRemoveUsername() != null
				&& (cla.getNewUsername() != null
						|| cla.getDisableUsername() != null || cla
						.getEnableUsername() != null)) {
			return true;
		} else {
			return false;
		}
	}
}
